package projectx.transaction;

import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUnexpectedException;
import projectx.core.base.ProjectXUserException;

public final class TransactionHandler {

	public static Object call(Transaction trx) throws ProjectXUserException,
			ProjectXSystemException {
		try {

			trx.initialize();

			Object ret = trx.execute();

			trx.commit();

			return ret;
		} catch (ProjectXUserException e) {
			trx.rollback();
			throw e;
		} catch (ProjectXSystemException e) {
			trx.rollback();
			throw e;
		} catch (Throwable e) {
			trx.rollback();
			throw new ProjectXUnexpectedException(e);
		} finally {
			trx.release();
		}
	}

	// --------------------------------------------------------------------------
	// C O N S T R U C T O R
	//
	// --------------------------------------------------------------------------
	private TransactionHandler() {
	}
}
